package com.shiyi.service.pdf.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;

import java.util.List;

@Slf4j
public class FileRedWordDynamic {

    private XWPFDocument doc;
    private CustomTOC toc;
    private Object wordSetting;


    public FileRedWordDynamic(XWPFDocument doc) {
        this.doc= doc;

    }

    public void writeFileHead() {
        // 添加标题和正文内容
        XWPFParagraph paragraph = doc.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText("这是正文内容。");
    }

    public void createCustomTOC() {
        CTSdtBlock block = doc.getDocument().getBody().addNewSdt();
        this.toc = new CustomTOC(block);
        doc.createTOC();
//        wordSetting.setCustomHeadingStyle(doc, "Heading1", 1);
//        wordSetting.setCustomHeadingStyle(doc, "Heading2", 2);
    }

    public void writeAllNews(List newsData) {
        log.info("开始写入目录");
        for (int i = 0; i < 10; i++) {
            // 添加标题和正文内容
            toc.addRowOnlyTitle(1,"1级title"+i);
            for (int j = 0; j < 10; j++) {
                toc.addRow(2, "2级title"+j,j,"第"+j+"章 引言");
            }
        }
        // 插入分页符
        XWPFParagraph p2 = doc.createParagraph();
        XWPFRun r2 = p2.createRun();
        // 分页符需要特定的字符，这里使用"\f"作为分页符
        r2.setText("\f");
        r2.setFontSize(2); // 设置字号为2，使得分页符不可见


        XWPFParagraph paragraph = doc.createParagraph();
        XWPFRun run = paragraph.createRun();
        log.info("开始写入正文");
        for (int i = 0; i < 10; i++) {
            // 添加标题和正文内容
            paragraph = doc.createParagraph();
            run = paragraph.createRun();
            run.setText("第"+i+"章 引言");
            run.setBold(true);
            paragraph.setStyle("Heading1");

            paragraph = doc.createParagraph();
            run = paragraph.createRun();
            run.setText("这是第"+i+"章的内容");
            for (int z = 0; z < 10; z++) {
                paragraph = doc.createParagraph();
                run = paragraph.createRun();
                run.setText("我是内容"+z+"/n");
            }
        }
    }

    public void addItem2TOC() {
        List<XWPFParagraph> paragraphs = doc.getParagraphs();
        for (XWPFParagraph par : paragraphs) {
            String parStyle = par.getStyle();
            if (parStyle != null && parStyle.startsWith("Heading")) {
                List<CTBookmark> bookmarkList=par.getCTP().getBookmarkStartList();
                try {
                    int level = Integer.parseInt(parStyle.substring("Heading".length()));
                    if(level==1){
                        //添加栏目
                        toc.addRowOnlyTitle(level, par.getText());
                    }else{
                        //添加标题
                        toc.addRow(level, par.getText(), 1, bookmarkList.get(0).getName());
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
